﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BYRBBS.Model;
using BYRBBS.Util;
using System.IO;
using System.Diagnostics;
using System.Xml.Linq;

namespace BYRForum
{
    public partial class EditMailPage : PhoneApplicationPage
    {
        string sendId;
        string title;
        string content;
        public EditMailPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            
            string source_page = NavigationContext.QueryString["from"];
            if (source_page == "reply")
            {
                textBoxSendId.Text = NavigationContext.QueryString["sendId"];
                textBoxTitle.Text = NavigationContext.QueryString["title"];
            }
            else if (source_page == "thread")
            {
                textBoxSendId.Text = NavigationContext.QueryString["sendId"];
            }
        }
        private void Send_Click(object sender, EventArgs e)
        {
            if (textBoxContent.Text == "" )
            {
                MessageBox.Show("收信人不能为空！");
                return;
            }
            sendId = textBoxSendId.Text;
            title = textBoxTitle.Text;
            content = textBoxContent.Text;
            String myRequestUrl = ByrApi.apiUrl + "/mail/send"+ ByrApi.Encode + ByrApi.appkey;
            HttpUtil.HttpPost(myRequestUrl, HttpWebRequset_Send_Callback);//发送请求
            progressBarWait.Visibility = Visibility.Visible;
        }
        private void HttpWebRequset_Send_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest hwrq = (HttpWebRequest)asynchronousResult.AsyncState;
                Stream responseStream = hwrq.EndGetRequestStream(asynchronousResult);
                StreamWriter sr = new StreamWriter(responseStream);

                sr.Write("id=" + sendId+ "&title=" + title +
                    "&content=" + content+"&backup=1");
                sr.Close();
                responseStream.Close();
                hwrq.BeginGetResponse(PostResponse_Callback, hwrq);
            }
            catch (Exception e)
            {
                Debug.WriteLine("POST STRING FAILED:" + e.Message);
            }
        }
        private void PostResponse_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                string result = HttpUtil.ReadToEnd(asynchronousResult);
                XDocument doc = XDocument.Parse(result);
                Debug.WriteLine(result);
                foreach (XElement item in doc.Descendants("error").Nodes())
                {
                    if (item.Name == "msg")
                    {
                        this.Dispatcher.BeginInvoke(
                            () =>
                            {
                                MessageBox.Show("发信失败：" + item.Value);
                                progressBarWait.Visibility = Visibility.Collapsed;

                            }
                            );


                        return;
                    }
                }
                this.Dispatcher.BeginInvoke(
                    () =>
                    {
                        MessageBox.Show("发信成功成功！");
                        progressBarWait.Visibility = Visibility.Visible;
                        this.NavigationService.GoBack();
                    }
                    );



            }
            catch { }



        }
    }
}